// 购物车状态
import { mergeLocalCart, findCartList, insertCart, deleteCart, updateCart, checkAllCart } from '@/api/cart'
// 购物车状态
export default {
  namespaced: true,
  state: () => ({
    list: [] // 购物车列表
  }),
  mutations: {
    // 加入购物车
    insertCart (state, palyLoad) {
      const index = state.list.findIndex(item => item.skuId === palyLoad.skuId)
      if (index > -1) {
        state.list[index].count = state.list[index].count + palyLoad.count
      } else {
        state.list.unshift(palyLoad)
      }
    },
    // 删除有效列表
    delCart (state, good) {
      const index = state.list.findIndex(item => item.skuId === good.skuId)
      if (index > -1) {
        state.list.splice(index, 1)
      }
    },
    // 单选功能
    singleCheck (state, { goods, selected }) {
      const item = state.list.find(item => item.skuId === goods.skuId)
      item.selected = selected
    },
    // 多选
    allCheck (state, selected) {
      state.list.forEach(item => {
        item.selected = selected
      })
    },
    // 修改数量
    changeCount (state, { good, count }) {
      const item = state.list.find(item => item.skuId === good.skuId)
      item.count = count
    },
    // 设置购物车列表
    setCartList (state, list) {
      state.list = list
    }
  },
  actions: {
    // 加入购物车
    asyncInsertCart (ctx, goods) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          insertCart({ skuId: goods.skuId, count: goods.count }).then(res => {
            findCartList().then((res) => {
              // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
              ctx.commit('setCartList', res.data.result)
              // 2.resolve
              resolve()
            })
          })
        } else {
          // 未登录
          ctx.commit('insertCart', goods)
          resolve()
        }
      })
    },
    // 删除
    asyncDelCart (ctx, goods) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          deleteCart([goods.skuId]).then(res => {
            findCartList().then((res) => {
              // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
              ctx.commit('setCartList', res.data.result)
              // 2.resolve
              resolve()
            })
          })
        } else {
          // 未登录
          ctx.commit('delCart', goods)
          resolve()
        }
      })
    },
    // 单选
    asyncSingleCheck (ctx, { goods, selected }) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          updateCart({ skuId: goods.skuId, count: goods.count, selected }).then(res => {
            findCartList().then((res) => {
              // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
              ctx.commit('setCartList', res.data.result)
              // 2.resolve掉
              resolve()
            })
          })
        } else {
          // 未登录
          ctx.commit('singleCheck', { goods, selected })
          resolve()
        }
      })
    },
    // 多选
    asyncAllCheck (ctx, selected) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          const ids = ctx.getters.validList.map(item => item.skuId)
          checkAllCart({ selected, ids }).then(res => {
            findCartList().then((res) => {
              // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
              ctx.commit('setCartList', res.data.result)
              // 2.resolve掉
              resolve()
            })
          })
        } else {
          // 未登录
          ctx.commit('allCheck', selected)
          resolve()
        }
      })
    },
    // 加减
    asyncChangeCount (ctx, { goods, count }) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          updateCart({ skuId: goods.skuId, count, selected: goods.selected }).then(res => {
            findCartList().then((res) => {
              // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
              ctx.commit('setCartList', res.data.result)
              // 2.resolve掉
              resolve()
            })
          })
        } else {
          // 未登录
          ctx.commit('changeCount', { goods, count })
          resolve()
        }
      })
    },
    // 合并购物车
    asyncMergeLocalCart (ctx) {
      const cartList = ctx.state.list.map(({ skuId, selected, count }) => {
        return { skuId, selected, count }
      })
      // 调用接口
      mergeLocalCart(cartList)
      // 获取购物车列表
      findCartList().then((res) => {
        // 1.把成功返回的最新的购物车列表交给我们本地的state中的list
        ctx.commit('setCartList', res.data.result)
      })
    },
    // 获取购物车列表
    asyncFindCartList (ctx) {
      return new Promise((resolve, reject) => {
        if (ctx.rootState.user.profile.token) {
          // 已登录
          findCartList().then(res => {
            ctx.commit('setCartList', res.data.result)
            resolve()
          })
        } else {
          // 未登录
        }
      })
    }
  },
  getters: {
    // 有效商品列表
    validList (state) {
      return state.list.filter(item => item.isEffective && item.stock !== 0)
    },
    // 有效商品总件数
    validCount (state, getters) {
      return getters.validList.reduce((a, c) => {
        return a + c.count
      }, 0)
    },
    // 有效商品总价格
    validAllPrice (state, getters) {
      return getters.validList.reduce((a, c) => {
        return a + (c.nowPrice * 100 * c.count) / 100
      }, 0)
    },
    // 全选getter
    allSelected (state) {
      return state.list.every(item => item.selected === true)
    },
    // 已选择商品总数
    selectedListCount (state) {
      // selected 为true
      return state.list.filter(item => item.selected === true).length
    },
    // 选中商品合计
    selectedAllPrice (state) {
      // 筛选出所有selected字段为true的项目
      // 把所有项目中的数量乘以nowPrice 进行叠加
      return state.list.filter(item => item.selected).reduce((a, c) => {
        return a + (c.count * c.nowPrice * 100) / 100
      }, 0)
    }
  }
}
